package com.jp.knowledge.my.activity;

import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.comm.SlidingActivity;
import com.jp.knowledge.my.adapter.BaseFraAdapter;
import com.jp.knowledge.my.business.MyReeceBusiness;

import org.xutils.view.annotation.ViewInject;

/**
 * 我的红包
 *
 * @author gzejia 978862664@qq.com
 */
public class MyReeceActivity extends SlidingActivity implements View.OnClickListener {

    @ViewInject(R.id.my_reece_tv_money)
    private TextView mMoneyTv;

    @ViewInject(R.id.my_reece_tv_get)
    private TextView mGetTv;

    @ViewInject(R.id.my_reece_tv_send)
    private TextView mSendTv;

    @ViewInject(R.id.my_reece_vp)
    private ViewPager mReeceVp;

    private MyReeceBusiness mBusiness = new MyReeceBusiness();

    @Override
    protected int getResourceId() {
        return R.layout.activity_my_reece;
    }

    @Override
    protected void init() {
        initTitle();
        initViewPager();
        initOthers();
        initClickListener();
    }

    private void initClickListener(){
        leftIcon.setOnClickListener(this);
        mGetTv.setOnClickListener(this);
        mSendTv.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.my_reece_tv_get:// 我收到的
                mReeceVp.setCurrentItem(0);
                break;
            case R.id.my_reece_tv_send:// 我发出的
                mReeceVp.setCurrentItem(1);
                break;
            case R.id.icon_left:// 返回
                finish();
                break;
        }
    }

    private void initTitle() {
        topName.setText(R.string.my_tv_reece);
        topDesc.setVisibility(View.GONE);
        leftIcon.setImageResource(R.mipmap.arrow_white_left);
        rightIcon.setVisibility(View.GONE);
    }

    private void initViewPager() {
        BaseFraAdapter fraAdapter = new BaseFraAdapter(getSupportFragmentManager(),
                mBusiness.getFragments(), mBusiness.getTabStr());
        mReeceVp.setAdapter(fraAdapter);
        mReeceVp.addOnPageChangeListener(mPageChange);
        mReeceVp.setOffscreenPageLimit(fraAdapter.getCount());
    }

    private void initOthers() {
        mMoneyTv.setText(String.valueOf(0.00));
    }

    /**
     * @param position 更新选中Tab标签状态
     */
    private void updateTabSelect(int position) {
        if (position == 0) {// 收到的
            mGetTv.setBackgroundResource(R.drawable.my_reece_tabl_press);
            mGetTv.setTextColor(getResources().getColor(R.color.white));
            mSendTv.setBackgroundResource(R.drawable.my_reece_tabr_normal);
            mSendTv.setTextColor(getResources().getColor(R.color.theme_color));
        } else {// 发出的
            mSendTv.setBackgroundResource(R.drawable.my_reece_tabr_press);
            mSendTv.setTextColor(getResources().getColor(R.color.white));
            mGetTv.setBackgroundResource(R.drawable.my_reece_tabl_normal);
            mGetTv.setTextColor(getResources().getColor(R.color.theme_color));
        }
    }

    ViewPager.OnPageChangeListener mPageChange = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
            updateTabSelect(position);
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };
}
